using System;
using System.Collections.Generic;
using System.Text;
using TTXNA.src.Util;
using TTXNA.src.Engine.World;
using Microsoft.Xna.Framework;
using TTXNA.src.Engine.Terrain;
using Microsoft.Xna.Framework.Graphics;


namespace TTXNA.src.Engine.Graphics
{
    public class TTWorld : TTUpdateable, TTRenderable
    {
        List<GameObject> pending = new List<GameObject>();

        List<TTObject> meshObjects = new List<TTObject>();
        List<GameObject> logicObjects = new List<GameObject>();

        TTTerrain terrain;

        public TTWorld()
        {

        }

        public void AddObject(GameObject obj)
        {
            pending.Add(obj);                
        }

        public void Update(GameTime gameTime)
        {
            AddNewObjects();

            for (int o = 0; o < meshObjects.Count; o++)
            {
                meshObjects[o].Update(gameTime);
            }
        }

        private void AddNewObjects()
        {
            foreach (GameObject obj in pending)
            {
                logicObjects.Add(obj);

                if ((obj is Map))
                {
                    Map map = (Map)obj;
                    terrain = new TTTerrain(map);
                    meshObjects.Add(terrain);
                }
            }

            //Model model = Model

            pending.Clear();
        }

        public void Render(TTCamera camera)
        {
            terrain.Render(camera);

            for (int o = 0; o < meshObjects.Count; o++)
            {
                meshObjects[o].Render(camera);
            }
        }

        public TTTerrain TerrainMesh
        {
            get { return terrain; }
        }
    }
}
